﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Animation;
using PairingMatrix.Common.Enum;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class ShowOrHideTeamMemberCardBehavior : Behavior<TeamMemberCardControl>
    {
        private ContentPresenter _parent;
        private TeamMemberCardViewModel _teamMemberCardViewModel;
        private Storyboard _storyboard;
        private DoubleAnimation _opacityAnimation;
        private double _finalOpacity;

        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
            {
                _teamMemberCardViewModel = (TeamMemberCardViewModel)AssociatedObject.DataContext;
                _teamMemberCardViewModel.PropertyChanged += TeamMemberCardPropertyChanged;

                if (_parent == null)
                {
                    _parent = (ContentPresenter)VisualTreeHelper.GetParent(AssociatedObject);
                }

                var duration = new Duration(TimeSpan.FromMilliseconds(400));
                _opacityAnimation = new DoubleAnimation();
                _opacityAnimation.Duration = duration;


                _storyboard = new Storyboard();
                _storyboard.Completed += new EventHandler(_storyboard_Completed);
                _storyboard.Duration = duration;

                _storyboard.Children.Add(_opacityAnimation);

                Storyboard.SetTarget(_opacityAnimation, _parent);

                Storyboard.SetTargetProperty(_opacityAnimation, new PropertyPath("Opacity"));
                
                HideCardIfNotInCurrentWall();
           
            };

        }

        void _storyboard_Completed(object sender, EventArgs e)
        {
            if (_finalOpacity == 1.0)
                AssociatedObject.Visibility = Visibility.Visible;
            else if (_finalOpacity == 0.0)
                AssociatedObject.Visibility = Visibility.Collapsed;

        }

        void TeamMemberCardPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "HasSignUpsOnCurrentWall" || e.PropertyName == "ArrangeCardToShowStatistics" || e.PropertyName == "ShowCardInAbsenteesBox" || e.PropertyName == "ArrangeCardToShowIceCreamMeter")
            {
                HideCardIfNotInCurrentWall();
            }
        }

        private void HideCardIfNotInCurrentWall()
        {
            if (_teamMemberCardViewModel.HasSignUpsOnCurrentWall || _teamMemberCardViewModel.ArrangeCardToShowStatistics || _teamMemberCardViewModel.ShowCardInAbsenteesBox || _teamMemberCardViewModel.ArrangeCardToShowIceCreamMeter)
            {
                _opacityAnimation.To = 1;
                _finalOpacity = 1;
                _storyboard.Begin();
            }
            else
            {
                _opacityAnimation.To = 0;
                _finalOpacity = 0;
                _storyboard.Begin();
            }
        }
    }
}
